package com.example.resource_station_backend.service.impl;


import com.example.resource_station_backend.dao.ArticlesRepository;
import com.example.resource_station_backend.dao.CategoryRepository;
import com.example.resource_station_backend.exception.ErrorCode;
import com.example.resource_station_backend.exception.ThrowUtils;
import com.example.resource_station_backend.model.dto.articles.ArticlesAddRequest;
import com.example.resource_station_backend.model.entity.Articles;
import com.example.resource_station_backend.model.entity.Category;
import com.example.resource_station_backend.model.vo.ArticlesVO;
import com.example.resource_station_backend.service.ArticlesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Optional;

/**
 * @Author xinhao
 * @Description
 * @CreateTime 2025-02-25 17:09:09
 */
@Service
public class ArticlesServiceImpl implements ArticlesService {

    @Autowired
    CategoryRepository categoryRepository;

    @Autowired
    ArticlesRepository articlesRepository;

    @Override
    public Articles voToObj(ArticlesVO articlesVO) {
        if (articlesVO == null) {
            return null;
        }
        // 通过分类名称找到对应的分类id
        Optional<Category> optionalCategory = categoryRepository.findIdByName(articlesVO.getCategory());
        ThrowUtils.throwIf(optionalCategory.isEmpty(), ErrorCode.PARAMS_ERROR);
        // 转换
        Articles articles = new Articles();
        BeanUtils.copyProperties(articlesVO, articles);
        // 分类的类型不同
        articles.setCategory(optionalCategory.get());
        return articles;
    }

    @Override
    public ArticlesVO objToVo(Articles articles) {
        if (articles == null) {
            return null;
        }
        // 通过分类id找到对应的分类名称
        Long categoryId = articlesRepository.findById(articles.getId()).get().getCategory().getId();
        Optional<Category> optionalCategory = categoryRepository.findById(categoryId);
        ThrowUtils.throwIf(optionalCategory.isEmpty(), ErrorCode.PARAMS_ERROR);
        String categoryName = optionalCategory.get().getName();
        // 转换
        ArticlesVO articlesVO = new ArticlesVO();
        BeanUtils.copyProperties(articles, articlesVO);
        // 分类的类型不同
        articlesVO.setCategory(categoryName);
        return articlesVO;
    }

    @Override
    public Long addArticle(ArticlesAddRequest articlesAddRequest) {
        if (articlesAddRequest.getId() == null) {
            articlesAddRequest.setScraped_at(LocalDateTime.now());
            if (articlesAddRequest.getPublished_at() == null) {
                articlesAddRequest.setPublished_at(LocalDateTime.now());
            }
        }
        // 通过分类名称找到对应的分类id
        Optional<Category> optionalCategory = categoryRepository.findById(articlesAddRequest.getCategory_id());
        ThrowUtils.throwIf(optionalCategory.isEmpty(), ErrorCode.PARAMS_ERROR);
        Articles articles = new Articles();
        BeanUtils.copyProperties(articlesAddRequest, articles);
        articles.setCategory(optionalCategory.get());

        // 插入数据库
        Articles savedArticles = articlesRepository.save(articles);
        ThrowUtils.throwIf(savedArticles == null, ErrorCode.OPERATION_ERROR, "添加文章失败");
        return savedArticles.getId();
    }


}
